﻿#region Copyright

//  ======================================================
//      Copyright (c) 2011 Toulr All rights reserved.
//     
//      The use and distribution terms for this software are contained in the file
//      named license.txt, which can be found in the root of this distribution.
//      By using this software in any fashion, you are agreeing to be bound by the
//      terms of this license.
//     
//     You must not remove this notice, or any other, from this software.
//  ======================================================

#endregion

#region Using Namespaces

using System.Diagnostics;
using System.Security.Cryptography;
using NUnit.Framework;
using Toulr.Util.Security;
using Toulr.Util.Text;

#endregion

namespace Toulr.Test.Util
{
    /// <summary>
    ///   测试安全相关操作类
    /// </summary>
    [TestFixture]
    public class SecurityFixture
    {
        /// <summary>
        ///   对称加密方法测试
        /// </summary>
        [Test]
        public void SymmetricalTest()
        {
            ISymmetrical symmetrical = new Symmetrical(Symmetrical.EncryptTypes.Rc2);
            //初始化向量
            byte[] key = symmetrical.GetKey();
            //密匙
            byte[] iv = symmetrical.GetIv();
            //输出生成的密钥和初始化向量
            Debug.Print(Encoding.Hex(key));
            Debug.Print(Encoding.Hex(iv));

            byte[] encrypt = symmetrical.Encrypt(key, iv, CipherMode.CBC, PaddingMode.Zeros,
                                                 Encoding.GetBytes("20110109$sinory@qq.com"));
            Assert.NotNull(encrypt, "加密失败");
            string enStr = Encoding.Hex(encrypt).Replace("-","%");
            Debug.Print(enStr);
            byte[] dencrypt = symmetrical.Dencrypt(key, iv, CipherMode.CBC, PaddingMode.Zeros, Encoding.Hex2Byte(enStr.Replace("%","-")));
            Assert.AreEqual("20110109$sinory@qq.com", Encoding.GetString(dencrypt).Trim('\0'));
        }
        /// <summary>
        /// 测试密码散列及加密
        /// </summary>
        [Test]
        public void PasswordEncodeTest()
        {
            string pwd = "sinory";
            string hex = Encoding.Hex(Encoding.Md5(pwd)).Replace("-", "%");
            Assert.Pass("密码散列成功："+hex);
        }
    }
}